//
//  ZYCTeaLeaveRecordRcvHeaderView.m
//  ZDSC
//
//  Created by 张威 on 2021/10/28.
//

#import "ZYCTeaLeaveRecordRcvHeaderView.h"
#import "ZYCTeaLeaveRcvStatusCollectionCell.h"
#import "ZYCTeaAttendanceRcvModel.h"
@interface ZYCTeaLeaveRecordRcvHeaderView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end
@implementation ZYCTeaLeaveRecordRcvHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}

#pragma mark - UICollectionViewDataSource && UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZYCTeaLeaveRcvStatusCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZYCTeaLeaveRcvStatusCollectionCell class]) forIndexPath:indexPath];
    ZYCTeaAttendanceRcvModel *model = self.dataSource[indexPath.item];
    if (model.selected) {
        cell.statusLabel.backgroundColor = HEX_RGB(@"#009B4C");
        cell.statusLabel.textColor = HEX_RGB(@"#FFFFFF");
    }else{
        cell.statusLabel.backgroundColor = HEX_RGB(@"#D6D6D6");
        cell.statusLabel.textColor = HEX_RGB(@"999898");
    }
    cell.statusLabel.text = model.name;
    return cell;
}
//
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    for (ZYCTeaAttendanceRcvModel *m in self.dataSource) {
        m.selected = NO;
    }
    ZYCTeaAttendanceRcvModel *model = self.dataSource[indexPath.item];
    model.selected = YES;
    [self.collectionView reloadData];
    
    
    if ([self.delegate respondsToSelector:@selector(teaLeaveRecordRcvHeaderViewDidReceiveFlag:)]) {
        [self.delegate teaLeaveRecordRcvHeaderViewDidReceiveFlag:model.status];
    }
}


- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{

}

#pragma mark - getters

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
        {
            ZYCTeaAttendanceRcvModel *model = [[ZYCTeaAttendanceRcvModel alloc] init];
            model.name = @"待处理";
            model.status = @"1";
            model.selected = YES;
            [_dataSource addObject:model];
        }
        {
            ZYCTeaAttendanceRcvModel *model = [[ZYCTeaAttendanceRcvModel alloc] init];
            model.name = @"已处理";
            model.status = @"2";
            model.selected = NO;
            [_dataSource addObject:model];
        }
        {
            ZYCTeaAttendanceRcvModel *model = [[ZYCTeaAttendanceRcvModel alloc] init];
            model.name = @"抄送";
            model.status = @"3";
            model.selected = NO;
            [_dataSource addObject:model];
        }
    }
    return _dataSource;
}
- (UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.itemSize = CGSizeMake(78.0f, 27.0f);
        _flowLayout.minimumLineSpacing = 15.0f;
        _flowLayout.minimumInteritemSpacing = 15.5f;
        _flowLayout.sectionInset = UIEdgeInsetsMake(15.5f, 0.0f, 15.5f, 0.0f);
        
    }
    return _flowLayout;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = HEX_RGB(@"#FAFAFA");
        [_collectionView registerClass:[ZYCTeaLeaveRcvStatusCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([ZYCTeaLeaveRcvStatusCollectionCell class])];
    }
    return _collectionView;
}


@end
